package com.zzy.transaction.config;

import com.zzy.transaction.aop.CustomTransactionAttributeSource;
import com.zzy.transaction.aop.TransactionAdvisor;
import com.zzy.transaction.aop.TransactionPointcutAdvisor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;

@ConditionalOnClass(name = {"org.springframework.transaction.annotation.Transactional"})
@AutoConfigureAfter(org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration.class)
public class TransactionAutoConfiguration {

    @Bean
    public TransactionPointcutAdvisor transactionProxyCreator() {
        CustomTransactionAttributeSource transactionAttributeSource = new CustomTransactionAttributeSource();
        TransactionPointcutAdvisor transactionPointcutAdvisor = new TransactionPointcutAdvisor(transactionAttributeSource);
        transactionPointcutAdvisor.setAdvice(new TransactionAdvisor(transactionAttributeSource));
        transactionPointcutAdvisor.setOrder(Ordered.LOWEST_PRECEDENCE);
        return transactionPointcutAdvisor;
    }
}
